################################################################################
#this is a script to batch process species distribution models on the hpc setup

#script was written by Jeremy VanDerWal (jjvanderwal@gmail.com), 27 November 2008

################################################################################
#set some constants

#set the working dir and output dir
working.dir = "/home1/99/jc152199/MAXENT/"
output.dir = "/home1/99/jc152199/MAXENT/output/"
setwd(working.dir)

#get the unique species
species = c('SAPCZEC','SAPTETR','CARRUBR','LAMCOGG','GNYQUEE','SAPBASI','LAMROBE')

#cycle through each species
for (spp in species) 

	{
	
	for (mod.run in c('BIOCLIM','microCLIM')) # Cycle through the two models for each species
	
		{
		
		if(mod.run=='microCLIM') # Set constants (occur file, background file, and projection directory for microCLIM run
		
		{
	
		#species occurrence files
		occur.file = '/home1/99/jc152199/MAXENT/occurs/microCLIM_Occurrence_SWD.csv'
		occur = read.csv(occur.file)
		occur = na.omit(occur)

		#background swd file
		background.file = '/home1/99/jc152199/MAXENT/bkgd/microCLIM_Background_SWD.csv'

		#directory of ASCII files for spatial projection
		proj.dir = '/home1/99/jc152199/MAXENT/ASCII/microCLIM/'
		
		}
		
		if(mod.run=='BIOCLIM') # Set constants (occur file, background file, and projection directory for BIOCLIM run
		
		{
	
		#species occurrence files
		occur.file = '/home1/99/jc152199/MAXENT/occurs/BIOCLIM_Occurrence_SWD.csv'
		occur = read.csv(occur.file)
		occur = na.omit(occur)

		#background swd file
		background.file = '/home1/99/jc152199/MAXENT/bkgd/BIOCLIM_Background_SWD.csv'

		#directory of ASCII files for spatial projection
		proj.dir = '/home1/99/jc152199/MAXENT/ASCII/BIOCLIM/'
		
		}
		
		#create a folder for the species
		spp.outfolder = paste(output.dir,spp,'/',mod.run,'/output/',sep="")
		spp.folder = paste(output.dir,spp,"/",mod.run,'/',sep="")
		dir.create(spp.outfolder, showWarnings=T, recursive=T)
		  
		#get the occurrances associated with the species
		t.occur = occur[occur$spp==spp,]
		
		#write out the occur files file
		write.csv(t.occur,paste(spp.folder,"occur.csv",sep=""),row.names=F)
		
		#copy over the maxent.jar file
		file.copy("maxent.jar",paste(spp.folder,"maxent.jar",sep=""),overwrite=T) 

		#create the pbs script
		
		z = file(paste(spp.folder,"PBS_script.pbs",sep=""),"w")
		
			cat('#!/bin/bash\n',file=z)
			cat('#PBS -c s\n',file=z)
			cat('#PBS -j oe\n',file=z)
			cat('#PBS -m ae\n',file=z)
			cat('#PBS -N maxent_',spp,'\n',sep="",file=z)
			cat('#PBS -M jc152199@jcu.edu.au\n',file=z)
			cat('#PBS -l walltime=9999:00:00\n',file=z)
			cat('#PBS -l nodes=1:ppn=2\n',file=z)
			cat('echo "------------------------------------------------------"\n',file=z)
			cat('echo " This job is allocated 2 cpu on "\n',file=z)
			cat('cat $PBS_NODEFILE\n',file=z)
			cat('echo "------------------------------------------------------"\n',file=z)
			cat('echo "PBS: Submitted to $PBS_QUEUE@$PBS_O_HOST"\n',file=z)
			cat('echo "PBS: Working directory is $PBS_O_WORKDIR"\n',file=z)
			cat('echo "PBS: Job identifier is $PBS_JOBID"\n',file=z)
			cat('echo "PBS: Job name is $PBS_JOBNAME"\n',file=z)
			cat('echo "------------------------------------------------------"\n',file=z)
			cat('cd $PBS_O_WORKDIR\n',file=z)
			cat('#run the model\n',file=z)
			cat('java -mx2500m -jar maxent.jar outputdirectory="',spp.outfolder,'" samplesfile="occur.csv" projectionlayers="',proj.dir,'" environmentallayers="',background.file,'"  togglelayertype="BVG" noskipifexists writeplotdata outputgrids plots warnings noaskoverwrite responsecurves jackknife novisible autorun','\n',sep="",file=z)
			cat('\n',file=z)
			
		close(z)
		  
		#change the working directory and submit the pbs script
		setwd(spp.folder)
		system("qsub PBS_script.pbs")
		system("sleep 2")
		
		#reset the working directory
		setwd(working.dir)
		
		# report progress
		cat('\n',spp,' ',mod.run,' Submitted\n',sep='')
		
		}
		
		# Close model run loop
		
	}
	
# Close spp loop



